package com.example.sinoyd.frameapplication.ProjectCodes.Fragment

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.os.Bundle
import android.os.Message
import android.support.annotation.RequiresApi
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.Chartdata
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.GridviewColumdata
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.gson
import com.example.sinoyd.frameapplication.KotlinFrame.Dialog.DateTimePickerControl
import com.example.sinoyd.frameapplication.KotlinFrame.Frame.Uitl.Networkrequestmodel
import com.example.sinoyd.frameapplication.KotlinFrame.MpChart.CombinedChartDrawing
import com.example.sinoyd.frameapplication.KotlinFrame.Uitl.showdialog
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetLatestHourAQI
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetMonitData
import com.example.sinoyd.frameapplication.R
import com.sinoyd.Code.MpChart.colorsbar
import com.sinoyd.Code.Until.Networkrequestaddress
import com.sinoyd.environmentsz.Kotlin.getToday
import kotlinx.android.synthetic.main.aqi_fragment.*
import kotlinx.android.synthetic.main.include_title_layout.*
import okhttp3.Response
import org.jetbrains.anko.act
import org.jetbrains.anko.onClick
import org.jetbrains.anko.runOnUiThread
import java.util.*

/**
 * AQI指数界面
 * **/
@SuppressLint("ValidFragment")
class AQIFragment(var name: String = "AQI指数") : BaseFragment() {
    var DataType = "Hour"
    var date = Date()
    var stime = "2018/5/20"
    var etime = "2018/5/20"
    var codelist = arrayOf("SO2", "NO2", "CO", "O3", "PM2.5", "PM10")
    var code = codelist[0]
    val message = Message.obtain()


    //选择站点后进行的操作
    override fun selectPortCallBack() {
        getLatestHourAQI()
        getMonitData()
        iv_left_two.text = portname
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val conview = inflater.inflate(R.layout.aqi_fragment, null)
        return conview
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setview()
        setlisterens()
        getLatestHourAQI()
        getMonitData()
    }


    //设置界面
    private fun setview() {
        center_title.text = name
        tv_showtime.text = date.getToday()
    }

    //设置各种监听
    private fun setlisterens() {
        //选择框
        aqiselectionbarview.setdefaulttext("小时数据", "日数据")
                .setdefaultSelectBt(aqiselectionbarview.LEFTTRUE)
                .settextcolor("#FFFFFF")
                .setradiisleft(floatArrayOf(10f, 10f, 0f, 0f, 0f, 0f, 10f, 10f))
                .setradiisright(floatArrayOf(0f, 0f, 10f, 10f, 10f, 10f, 0f, 0f))
                .setradiobg("#550f82ff", "#0f82ff")
                .setOnCheckedChangeListener { radioGroup, i ->
                    when (i) {
                        R.id.rb_bt1 -> {
                            stime = "2018/5/20"
                            etime = "2018/5/20"
                            code = codelist[0]
                            tv_showtime.visibility = View.GONE
                            DataType = "Hour"
                            aqiCombinedChart.visibility = View.VISIBLE
                            batchartview.visibility = View.GONE
                            getMonitData()
                        }
                        R.id.rb_bt2 -> {
                            code = ""
                            tv_showtime.visibility = View.VISIBLE
                            DataType = "Day"
                            aqiCombinedChart.visibility = View.GONE
                            batchartview.visibility = View.VISIBLE
                            getMonitData()
                        }
                    }
                }
        //时间选择器监听
        tv_showtime.onClick {
            var dateDialog = DateTimePickerControl(activity, AlertDialog.THEME_HOLO_LIGHT, "date", "#0f82ff")
            dateDialog.SetDateSelectListener { dateString ->
                tv_showtime.text = dateString
                stime = dateString
                etime = dateString
                //发请求
                getMonitData()
            }
            dateDialog.show()
        }

    }


    //获取最新小时AQI空气情况
    fun getLatestHourAQI() {
        showdialog(act, "loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("getLatestHourAQI")
                .seturl(Networkrequestaddress.URL_GetLatestHourAQI)
                .addparam("portId", portid)
                .start(this)
    }

    //【查询监测数据】获取24小时浓度数据
    fun getMonitData() {
        showdialog(act, "loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("getMonitData")
                .seturl(Networkrequestaddress.URL_GetMonitData)
                .addparam("portId", portid)
                .addparam("DataType", DataType)
                .addparam("stime", stime)
                .addparam("etime", etime)
                .addparam("code", code)
                .start(this)
    }

    var getLatestHourAQI = GetLatestHourAQI()
    var getMonitData = GetMonitData()
    var list图表: HashMap<String, ArrayList<Chartdata>> = HashMap()
    var list柱状: ArrayList<GridviewColumdata> = ArrayList()
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    override fun requestSuccess(response: Response, TAG: String) {
        super.requestSuccess(response, TAG)

        when (TAG) {
            "getLatestHourAQI" -> {
                getLatestHourAQI = gson.fromJson(responsestr, GetLatestHourAQI::class.java)

                if (getLatestHourAQI.portHourAQI == null || getLatestHourAQI.portHourAQI.size == 0) {
                    runOnUiThread {
                        tvAQI.text = "X"
                        tvClass.text = "?"
                        tvPrimaryName.text = "--"
                        tvPrimaryValue.text = "--"
                        tvAQI.setTextColor(Color.rgb(0, 0, 0))
                        lv2AQI.setBackgroundDrawable(resources.getDrawable(R.drawable.frame_shape_colum_gv))
                    }
                } else {
                    runOnUiThread {
                        tvAQI.text = getLatestHourAQI.portHourAQI[0].aqi
                        tvClass.text = getLatestHourAQI.portHourAQI[0].classname
                        tvPrimaryName.text = getLatestHourAQI.portHourAQI[0].primaryPollutant
                        tvPrimaryValue.text = getLatestHourAQI.portHourAQI[0].primaryPollutantValue
                        var color = colorsbar[0]
                        val gard = GradientDrawable()
                        when (getLatestHourAQI.portHourAQI[0].grade) {
                            "一级" -> {
                                color = colorsbar[0]
                            }
                            "二级" -> {
                                color = colorsbar[1]
                            }
                            "三级" -> {
                                color = colorsbar[2]
                            }
                            "四级" -> {
                                color = colorsbar[3]
                            }
                            "五级" -> {
                                color = colorsbar[4]
                            }
                            "六级" -> {
                                color = colorsbar[5]
                            }
                        }
                        gard.setColor(color)
                        gard.cornerRadii = floatArrayOf(100f, 100f, 100f, 100f, 100f, 100f, 100f, 100f)
                        lv2AQI.setBackgroundDrawable(gard)
                        tvAQI.setTextColor(color)
                    }
                }
//                else {
//                    //                    Looper.prepare()
////                    toast("未获取到AQI情况数据")
////                    Looper.loop()
//                    //恢复默认界面
//
//
//                }
            }
            "getMonitData" -> {
                getMonitData = gson.fromJson(responsestr, GetMonitData::class.java)
                //整理数据
                var list: ArrayList<Chartdata> = ArrayList()
                list柱状 = ArrayList()
                when (DataType) {
                    "Hour" -> {

                        if (getMonitData.historyData == null || getMonitData.historyData.size == 0) {
                            runOnUiThread {
                                tv_message.visibility = View.VISIBLE
                                aqiCombinedChart.visibility = View.GONE
                            }
                        } else {
                            runOnUiThread {
                                tv_message.visibility = View.GONE
                                aqiCombinedChart.visibility = View.VISIBLE
                            }

                            for (item in getMonitData.historyData) {
                                list.add(Chartdata(item.dateTime, item.value[0].value))
                            }
                            list图表[code] = list
                            show24chart(list图表)
                        }


                    }
                    "Day" -> {

                        if (getMonitData.historyData == null || getMonitData.historyData.size == 0) {
                            runOnUiThread {
                                batchartview.visibility = View.GONE
                                tv_message.visibility = View.VISIBLE
                            }
                        } else {
                            runOnUiThread {
                                batchartview.visibility = View.VISIBLE
                                tv_message.visibility = View.GONE
                            }

                            for (item in getMonitData.historyData[0].value) {
                                list柱状.add(GridviewColumdata(item.factor, item.value.toDouble(), item.color))
                            }
                            runOnUiThread {
                                batchartview.setcolors("#990f82ff", "#990f82ff", "#0f82ff").setroundRadius(100f).show(list柱状)
                            }
                        }


                    }
                }

            }
        }
    }


    //显示24小时浓度图表
    fun show24chart(list图表: HashMap<String, ArrayList<Chartdata>>) {
        runOnUiThread {
            var ccd = CombinedChartDrawing(act, aqiCombinedChart, list图表, CombinedChartDrawing.ONLY_BROKEN_LINE, false, true, true, false)
            ccd.initview().showchart()
        }
    }

}